﻿/// <reference path="jquery/jquery-1.4.1-vsdoc.js" />

$(function () {
    PrepareInfoDisplay();
    $('.addcontactbutton').click(function () {
        var box = $(this).parents('.hsrt');
        $('#shs-c-avt').empty();
        $('#shs-c-nn').empty();
        $('#shs-c-cmt').val("");
        $('#shs-c-uid').val('');
        box.find('.avatarthumbcontainer').clone().appendTo('#shs-c-avt');
        box.find('a[rel=nickname]').clone().appendTo('#shs-c-nn');
        $('#shs-c-cmt').Watermark("好友请求附言");
        $('div#shs-c').dialog('open');
        $('#shs-c-uid').val($(this).attr('uid'));
        $('#sendcontactrequestbutton').show().click(SendContactRequestFromHS);
    });
});

function PrepareInfoDisplay() {
    //alert(Utility.RenderAvatar);
    Utility.DisplayAvatar(function () {
        $('div.namebox').each(function () {
            $(this).parents('.inf').find('.nn').parent().replaceWith($(this));
        });
    });
    //Utility.RenderAvatar();

    $('div#shs-c').dialog({
        modal: true,
        width: 350,
        height: 'auto',
        autoOpen: false,
        resizable: false,
        draggable: false
    });

    $('div#postsend').hide();
    $('div#presend').show();
}

function SendContactRequestFromHS() {
    var commentel = $('#shs-c-cmt');
    commentel.removeClass('commentnotempty');
    var comment = $.trim(commentel.val());
    if ("" == comment) {
        commentel.addClass('commentnotempty');
        commentel.focus();
        return false;
    }
    else {
        $(this).hide();
        var requesteeUserId = $('#shs-c-uid').val();
        $.ajax({
            type: 'POST',
            url: relRootPath + '/Mod/Contact/ContactWebService.asmx/SendContactRequest',
            data: {
                requestorUserId: userid,
                requesteeUserId: requesteeUserId,
                comment: comment
            },
            success: function (data, t, h) {
                // Modify here
                $('div#postsend').show();
                $('div#presend').hide();
                $('a[uid=' + requesteeUserId + ']').text("请求已发送").unbind('click');
                $('.closesendrequestwindow').click(function () {
                    $('div#shs-c').dialog('close');
                });
            },
            error: function (a, b, c) {
                alert('error');
            }
        });

        return true;
    }
}